<?php

namespace App\Models\Order;

use App\Models\Currency;
use App\Models\Products\Product;
use App\Models\Products\Review;
use App\Models\ReturnProduct\ReturnProduct;
use App\Services\Customers\AppCustomer;
use App\Services\MobileApp\AppEnv;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;

/**
 * @property-read  Order $order
 * @property-read  int $product_id
 * Class OrderProduct
 * @package App\Models\Order
 * @property   Collection|OrderOption orderOptions
 * @property string name
 * @property string image
 * @property string symbol_left
 * @property string symbol_right
 * @property string price
 * @property string total
 * @property OrderOption order_options
 * @property Product|null productByOnSale
 * @property int selling_quantity - 总销量
 * @property bool has_review
 * @property string order_product_id
 * @property Review review
 * @property string order_id
 * @property Product product
 * @property bool has_return
 * @property ReturnProduct returnProduct
 * @property Order orderByPaid
 * @property string quantity
 *
 * @method static self filterCanBeEvaluate($order_status_ids, $customer_id)
 *
 * @mixin Builder
 */

class OrderProduct extends Model
{
    protected $table = 'order_product';
    protected $guarded = [];

    protected $primaryKey = 'order_product_id';

//    protected $appends = ['symbol_left', 'symbol_right'];

    public $currency;


    /**
     * OrderProduct constructor.
     * @param array $attributes
     * @throws \Exception
     */
    public function __construct(array $attributes = [])
    {
        parent::__construct($attributes);

        $this->currency = Currency::fromEnv();
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function product()
    {
        return $this->belongsTo(Product::class, 'product_id', 'product_id');
    }

    /**
     * @used-by $productByOnSale
     *
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function productByOnSale()
    {
        return $this->belongsTo(Product::class, 'product_id', 'product_id')
            ->where('status', Product::PRODUCT_STATUS_ON)
            ->where('date_available', '<=', now())
            ->whereHas('productToStore', function(Builder $query) {
                return $query->where('store_id', AppEnv::storeId());
            });
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function orderOptions()
    {
        return $this->hasMany(OrderOption::class, 'order_product_id', 'order_product_id');

    }

    public function order()
    {
        return $this->belongsTo(Order::class, 'order_id', 'order_id');
    }

    public function orderByPaid()
    {
        return $this->belongsTo(Order::class, 'order_id', 'order_id')
            ->where('order_status_id', '>', OrderStatus::ORDER_STATUS_ID_DEFAULT);
    }

    public function review()
    {
        return $this->belongsTo(Review::class, 'order_product_id', 'order_product_id');
    }

    public function returnProduct()
    {
        return $this->belongsTo(ReturnProduct::class, 'order_product_id', 'order_product_id');
    }

    /**
     * @return mixed|string
     */
//    public function getSymbolLeftAttribute()
//    {
//        $currency = $this->currency;
//
//        return $currency->symbol_left;
//    }

    /**
     * @return mixed|string
     */
//    public function getSymbolRightAttribute()
//    {
//        $currency = $this->currency;
//
//        return $currency->symbol_right;
//    }

    /**
     * @param OrderProduct $order_product
     * @return bool
     */
    public static function isHasReview(OrderProduct $order_product)
    {
        $review = (new Review)->where('order_product_id', $order_product->order_product_id)
            ->first();

        return (bool)$review;
    }

    /**
     * @param OrderProduct $order_product
     * @return bool
     */
    public static function canToBeEvaluate(OrderProduct $order_product)
    {
        $order_status_id = $order_product->order->order_status_id;

        $is_can_to_be_evaluate = OrderFlowStatus::checkIsToBeEvaluate($order_status_id);

        return $is_can_to_be_evaluate;
    }

    /**
     * @used-by filterCanBeEvaluate()
     *
     * @param Builder $query
     * @param $order_status_ids
     * @param $customer_id
     * @return Builder
     */
    public function scopeFilterCanBeEvaluate(Builder $query, $order_status_ids, $customer_id)
    {
        return $query->whereHas('order', function(Builder $query) use($order_status_ids, $customer_id){
            return $query->whereIn('order_status_id', $order_status_ids)
                ->where('customer_id', $customer_id);
        });

    }
}
